package com.tomvian.twodee.common 
{
	import flash.display.MovieClip;
	/**
	 * StateDisplay
	 * @author Thomas Vian
	 */
	public class StateDisplay
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		protected var _target:MovieClip;
		protected var _targetClip:MovieClip;
		
		protected var _path0:String;
		protected var _path1:String;
		protected var _path2:String;
		protected var _path3:String;
		
		protected var _pathDepth:uint;
		
		protected var _state:String;
		protected var _stateNum:uint;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		public function get state():String { return _state; }
		public function set state(value:String):void
		{
			if (_state != value)
			{
				_state = value;
				
				if (!_targetClip.stage) findTarget();
				
				_targetClip.gotoAndStop(_state);
			}
		}
		
		public function get stateNum():uint { return _stateNum; }
		public function set stateNum(value:uint):void
		{
			if (_stateNum != value)
			{
				_stateNum = value;
				
				if (!_targetClip.stage) findTarget();
				
				_targetClip.gotoAndStop(_stateNum);
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function StateDisplay(target:MovieClip, path:String = "") 
		{
			_target = target;
			var pathlist:Array = path.split(".");
			if (pathlist.length == 1 && pathlist[0] == "") pathlist = [];
			
			_pathDepth = pathlist.length;
			
			if (pathlist[0]) _path0 = pathlist[0];
			if (pathlist[1]) _path1 = pathlist[1];
			if (pathlist[2]) _path2 = pathlist[2];
			if (pathlist[3]) _path3 = pathlist[3];
			
			findTarget();
			
			_targetClip.gotoAndStop(1);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		protected function findTarget():void
		{
			switch(_pathDepth)
			{
				case 0:
				{
					_targetClip = _target;
					break;
				}
				case 1:
				{
					_targetClip = _target[_path0];
					break;
				}
				case 2:
				{
					_targetClip = _target[_path0][_path1];
					break;
				}
				case 3:
				{
					_targetClip = _target[_path0][_path1][_path2];
					break;
				}
				case 4:
				{
					_targetClip = _target[_path0][_path1][_path2][_path3];
					break;
				}
			}
		}
	}
}